# -*- coding: utf-8 -*-
from openerp.osv import fields
from openerp.osv import osv
from openerp import models, SUPERUSER_ID
from baidu_speech import AipSpeech
class baidu_cloud(osv.osv):
    _name = 'baidu.cloud'
    _columns = {
        'app_id': fields.char(u'APP_ID'),
        'api_key': fields.char(u'API_KEY'),
        'secret_key': fields.char(u'SECRET_KEY'),
    }

    def char_to_voice(self, cr, uid, target_txt, context=None):
        model_data_obj = self.pool.get('ir.model.data')
        baidu_voice_id = model_data_obj.xmlid_to_res_id(
            cr, SUPERUSER_ID, 'dftg_ext.dftg_baidu_voice')
        for baidu_row in self.browse(cr, uid, baidu_voice_id, context=context):
            aipSpeech = AipSpeech(baidu_row.app_id, baidu_row.api_key, baidu_row.secret_key)
            result = aipSpeech.synthesis(target_txt, 'zh', 1, {
                'vol': 5,
            })
            return result
            # APP_ID = '9872232' #使用范例
            # API_KEY = 'AhrNAw665Cjc5nKAI38LqwhF'
            # SECRET_KEY = 'jxGYlRcfla0HnG0Im5QCzqEei2lnn10m'
            # # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
            # if not isinstance(result, dict):
            #     with open('auido.mp3', 'wb') as f:
            #         f.write(result)